package com.cuitrip.business.order.detail.ui.viewholder;

import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.style.StrikethroughSpan;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.cuitrip.app.base.PartViewHolder;
import com.cuitrip.business.order.detail.ui.model.PricePartRenderData;
import com.cuitrip.business.tripservice.proxy.PriceProxy;
import com.cuitrip.business.tripservice.ui.IconTextView;
import com.cuitrip.service.R;
import com.cuitrip.util.ResourceUtil;
import com.lab.logtrack.AppLog;
import com.lab.utils.NumberUtils;

import butterknife.Bind;
import butterknife.ButterKnife;

import static com.cuitrip.business.order.detail.ui.model.PricePartRenderData.PriceMode;

/**
 * 订单信息行程viewholder
 * Created by ak
 */
public class PricePartViewHolder implements PartViewHolder<PricePartRenderData> {

    @Bind(R.id.first_item_price)
    TextView mFirstPrice;

    @Bind(R.id.second_item_price)
    TextView mSecondPrice;

    @Bind(R.id.third_item_price)
    TextView mThirdPrice;

    @Bind(R.id.fourth_item_price)
    TextView mFourthPrice;

    //集合费用
    @Bind(R.id.ct_order_total_price)
    TextView mFinalPrice;

    @Bind(R.id.first_item_layout)
    View mFirstItemLayout;

    @Bind(R.id.second_item_layout)
    View mSecondItemLayout;

    @Bind(R.id.third_item_layout)
    View mThirdItemLayout;

    @Bind(R.id.fourth_item_layout)
    View mFourthItemLayout;

    @Bind(R.id.first_item_title)
    TextView mFirstItemTitle;

    @Bind(R.id.second_item_title)
    TextView mSecondItemTitle;

    @Bind(R.id.total_title)
    TextView mTotalTitle;

    @Bind(R.id.total_bottom_line)
    View totalBottomLine;

    @Bind(R.id.total_block)
    RelativeLayout mTotalBlock;

    @Bind(R.id.arrow)
    IconTextView mArrow;

    @Bind(R.id.pay_fee)
    LinearLayout mPayFee;



    public void build(View view) {
        ButterKnife.bind(this, view);
    }

    public void render(final PricePartRenderData data) {
        PriceMode priceMode = data.getPriceMode();
        switch (priceMode){
            case Simple:
                totalBottomLine.setVisibility(View.VISIBLE);
                mPayFee.setVisibility(View.GONE);
                mFinalPrice.setText(PriceProxy.getInstance().priceStyledFormatText(data.getCurrency(),data.getPaidFee()));
                mTotalTitle.setText(R.string.price);
                mArrow.setVisibility(View.GONE);
                break;
            case Detail:
                totalBottomLine.setVisibility(View.GONE);
                mPayFee.setVisibility(View.VISIBLE);
                mTotalTitle.setText(R.string.fee_paid);
                mFinalPrice.setText(PriceProxy.getInstance().priceStyledFormatText(data.getCurrency(), data.getPaidFee()));
                mFirstPrice.setText(NumberUtils.getMoneyFormat(data.getServiceFee()));
                String serviceFeeIsCharged = data.getServiceFeeIsCharged();
                String tranFeeIsCharged = data.getTranFeeIsCharged();
                String transactionFee = data.getTransactionFee();
                String transactionFeeFormat = NumberUtils.getMoneyFormat(transactionFee);
                if ("-1".equals(tranFeeIsCharged)){
                    mSecondPrice.setVisibility(View.GONE);
                    mSecondItemLayout.setVisibility(View.GONE);
                }else if ("0".equals(tranFeeIsCharged) &&(!TextUtils.isEmpty(transactionFee))){
                    mSecondPrice.setVisibility(View.VISIBLE);
                    mSecondItemLayout.setVisibility(View.VISIBLE);
                    SpannableString tranFeeSpan = new SpannableString(transactionFeeFormat);
                    tranFeeSpan.setSpan(new StrikethroughSpan(), 0, transactionFeeFormat.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                    mSecondPrice.setText(tranFeeSpan);
                }else{
                    mSecondPrice.setVisibility(View.VISIBLE);
                    mSecondItemLayout.setVisibility(View.VISIBLE);
                    mSecondPrice.setText(transactionFeeFormat);
                }
                String platformFee = data.getPlatformFee();
                String platformFeeFormat = NumberUtils.getMoneyFormat(platformFee);
                if ("-1".equals(serviceFeeIsCharged)){
                    mThirdPrice.setVisibility(View.GONE);
                    mThirdItemLayout.setVisibility(View.GONE);
                }else if("0".equals(serviceFeeIsCharged) && !TextUtils.isEmpty(platformFee)){
                    mThirdPrice.setVisibility(View.VISIBLE);
                    mThirdItemLayout.setVisibility(View.VISIBLE);
                    SpannableString platformFeeSpan = new SpannableString(platformFeeFormat);
                    platformFeeSpan.setSpan(new StrikethroughSpan(), 0, platformFeeFormat.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                    mThirdPrice.setText(platformFeeSpan);
                }else{
                    mThirdItemLayout.setVisibility(View.VISIBLE);
                    mThirdPrice.setVisibility(View.VISIBLE);
                    mThirdPrice.setText(platformFeeFormat);
                }

                int coupon = 0;
                try{
                    coupon = (int) Float.parseFloat(data.getCoupon());
                }catch (Exception e){
                    AppLog.e(e.getMessage());
                }

                if (coupon == 0){
                    mFourthItemLayout.setVisibility(View.GONE);
                }else{
                    mFourthItemLayout.setVisibility(View.VISIBLE);
                    mFourthPrice.setText("- "+data.getCoupon());
                }
                setTotalItemExpand();
                break;
            case Refund:
                totalBottomLine.setVisibility(View.GONE);
                mTotalTitle.setText(R.string.fee_type_refund);
                mFinalPrice.setText(PriceProxy.getInstance().priceStyledFormatText(data.getCurrency(),data.getRefundFee()));
                mPayFee.setVisibility(View.VISIBLE);
                mFirstItemTitle.setText(R.string.fee_paid);
                mSecondItemTitle.setText(R.string.fee_type_cancellation);
                mFirstPrice.setText(data.getPaidFee());
                mSecondPrice.setText(data.getCancellationFee());
                mThirdItemLayout.setVisibility(View.GONE);
                mFourthItemLayout.setVisibility(View.GONE);
                setTotalItemExpand();
                break;
            default:

        }



    }

    private void setTotalItemExpand(){
        mTotalBlock.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mPayFee.getVisibility() == View.VISIBLE) {
                    mArrow.setText(ResourceUtil.getString(R.string.arrow_down_icon));
                    mPayFee.setVisibility(View.GONE);
                    totalBottomLine.setVisibility(View.VISIBLE);
                } else {
                    mArrow.setText(ResourceUtil.getString(R.string.arrow_up_icon));
                    mPayFee.setVisibility(View.VISIBLE);
                    totalBottomLine.setVisibility(View.GONE);
                }
            }
        });
    }


}
